package com.ktjy.web.dao.impl;

import com.ktjy.web.dao.BaseDao;
import com.ktjy.web.dao.GoodsDao;
import com.ktjy.web.dao.UserDao;
import com.ktjy.web.entity.GoodsItem;
import com.ktjy.web.entity.User;
import com.ktjy.web.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GoodsDaoImpl extends BaseDao implements GoodsDao {
    @Override
    public List<GoodsItem> selPageByName(String goodsName, int index, int pageSize) {
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        List<GoodsItem> list=null;
        try{
            list=sqlSession.getMapper(GoodsDao.class).selPageByName(goodsName,index,pageSize);
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return list;
    }

    @Override
    public GoodsItem selGoodsById(int id) {
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        GoodsItem goodsItem=null;
        try{
            goodsItem=sqlSession.getMapper(GoodsDao.class).selGoodsById(id);
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return goodsItem;
    }

    @Override
    public int updateGoods(GoodsItem goodsItem) {
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        int count=0;
        try{
            count=sqlSession.getMapper(GoodsDao.class).updateGoods(goodsItem);
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return count;
    }

    @Override
    public int delGoods(int id) {
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        int count=0;
        try{
            count=sqlSession.getMapper(GoodsDao.class).delGoods(id);
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return count;
    }

    @Override
    public int selGoodsCount() {
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        int count=0;
        try{
            count=sqlSession.getMapper(GoodsDao.class).selGoodsCount();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return count;
    }
}
